/**
* Edit
*/
(function($){
var

	/**
	* editComplete: When a tab has been submitted, load the result in the tab and bind the back button.
	*/
	editComplete = function(e,data,error,html){
		var tab=$(this),
			dlg=e.data.accordion.parent();
		// Set the reload flag...
		if ( data.reload ){
			dlg.attr("edit_reload",data.reload);
			// ... and add a (+) on the dialog title.
			dlg.dialog("option", "title", "(+) "+dlg.dialog("option", "title"));
		}
		// print an error, if any...
		if ( error ){
			alert( error);
		}
		tab.html(html)
			.find("button").bind("click",e.data,
						function(e){
							loadTab( e.data.accordion, e.data.tab_id);
						});
		Core.Refresh();
	},

	/**
	* loadTab: do the BoxRequest and populate the tab with the results. Also bind events and create the formRequest.
	*/
	loadTab = function(accordion, tab_id){
		var atom_id=accordion.attr("atom_id"),
			tabLoaded = function(e,data,error,html){
				var atom_id=e.data.atom_id,
					tab_id=e.data.tab_id;
				// Get the tab...
				$(this).find(">div[tab_id="+tab_id+"]")
							// Set the html code inside...
							.html(data.tab_html)
							// Get the "form" ...
							.find(">div:eq(0)")
									// ...and make it a formRequest object...
									.formRequest("BoxRequest", // request id
												{action:"save_tab", // request parameters
													atom_id:atom_id,
													tab_id:tab_id},
												editComplete, // callback
												{accordion:accordion, // callback data
												tab_id:tab_id})
									// then bind the submit button to submit the formRequest
									.find(">button:first")
											.bind("click", function(e){
																$(this).parent().trigger("request_submit");
																return false;
															});
				Core.Refresh();
				$(this).accordion("resize");
				return false;
			};
		accordion.one("tab_loaded", {tab_id:tab_id,atom_id:atom_id}, tabLoaded)
				.jsonRequest("BoxRequest", {action:"edit_tab",atom_id:atom_id,tab_id:tab_id}, "tab_loaded" );
	},

	/**
	* selectTab: select a tab, load it...
	*/
	selectTab = function(e,ui){
		loadTab($(this), ui.newContent.attr("tab_id") );
		return false;
	},

	/**
	* loadTabs: load list of tabs and initialize the accordion object
	*/
	loadTabs = function(e,data,error,html){
		var atom_id=e.data,
			accordion = $("<div></div>"),
			key=0;
		// Create all the tabs
		for(key in data.tabs){
			accordion.append( $("<h3><a href='#'>"+data.tabs[key]+"</a></h3><div></div>")
								.attr("tab_id", key));
		}
		// Initialize the accordion
		$(this).append( accordion.accordion({
								fillSpace: true,
								collapsible: true
								})
							.bind("accordionchange", selectTab)
							.attr("atom_id", atom_id) );
		// Load the initial tab
		loadTab( accordion, 0 );
		return false;
	},

	editStart = function(e){
		var box = e.data.box,
			atom_id = box.attr("atom_id");
		// Skip if editing window is already open
		if ( !box.data("edit_window") ){
			// Create the editing window...
			box.data("edit_window",
					$('<div></div>')
							.dialog({
								resize:function(){$(this).find(">div").accordion("resize");},
								closeOnEscape: false,
								title: e.data.title,
								width: 500,
								height: 500,
								close: function(e,u){box.data("edit_window",false);$(this).remove();},
								beforeclose: function(){
													if ($(this).attr("edit_reload")){
														box.boxDraw();
													}
													return true;
												}
								})
							.one("build_edit_tabs", atom_id, loadTabs )
							.jsonRequest("BoxRequest", {action:"list_tabs",atom_id:atom_id}, "build_edit_tabs" )
					);
		}
		return false;
	};


Core.ObjectTypes["div.edit_inline"] = function(){
	$(this).bind("click", {box:$(this).parent(),
							title:$(this).html()}, editStart );
};

Core.ObjectTypes["div.edit_popup"] = function(){
	var me=$(this),
		p=me.parent();
	$(this).bind("click", {box:p,
							title:$(this).html()}, editStart );
	p.bind("mouseenter",me , function(e){
			e.data.fadeIn("fast");
		}).bind("mouseleave", me, function(e){
			e.data.fadeOut("fast");
		});
};

}(jQuery));

